<template>
  <div class="worktask-designer-container">
    <!-- <top-tip :tipcontent="tipcontent" /> -->
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap :size="size">
            <el-form-item>
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="按客户名称"
                @input="trimkey"
              />
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryForm.OrderType"
                clearable
                placeholder="按合同属性"
                style="width: 150px"
              >
                <el-option
                  v-for="(item, index) in orderTypeOption"
                  :key="index"
                  :label="item.OrderTypeName"
                  :value="item.OrderTypeID"
                />
              </el-select>
            </el-form-item>
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.searchProduct"
                :options="ProductClassOptions"
                :props="ProductClassSelect"
                collapse-tags
                clearable
                placeholder="按产品"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.kefuStaff"
                clearable
                placeholder="按续签客服"
                style="width: 100px"
              />
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="queryForm.serviceDates"
                style="width: 240px"
                type="daterange"
                placeholder="按服务到期日期"
                start-placeholder="服务到期起"
                end-placeholder="服务到期止"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item
              v-if="
                queryForm.tab == 'serviceing' ||
                queryForm.tab == 'allServiceingCustomer'
              "
            >
              <el-select
                v-model="queryForm.isSerivceRecord"
                clearable
                placeholder="有无小记"
                style="width: 100px"
              >
                <el-option label="有小记" :value="1" />
                <el-option label="无小记" :value="-1" />
              </el-select>
            </el-form-item>
            <el-form-item
              v-if="
                queryForm.tab == 'serviceing' ||
                queryForm.tab == 'allServiceingCustomer'
              "
            >
              <el-date-picker
                v-model="queryForm.TrackRecordDates"
                style="width: 280px"
                type="daterange"
                placeholder="按服务小记查询"
                start-placeholder="服务小记日期起"
                end-placeholder="服务小记日期止"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane label="新分待运营任务" name="new">
        <el-table v-loading="listLoading" :data="list">
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
            fixed
          />
          <el-table-column
            align="center"
            prop="OrderID"
            label="合同ID"
            width="70"
            fixed
          />
          <el-table-column
            align="center"
            prop="OrderCode"
            label="合同编号"
            width="200"
          />
          <el-table-column align="center" prop="OrderType" label="合同属性" />
          <el-table-column align="center" label="派单时间" width="110">
            <template #default="{ row }">
              {{ row.AddTime.split(' ')[0] }}
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="200"
          /> -->
          <el-table-column align="left" label="客户名称" width="250">
            <template #default="{ row }">
              <div style="cursor: pointer" @click="getCustomerInfo(row)">
                {{ row.CustomerAllName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="KefuStaffName"
            label="续签客服"
            width="100"
          />
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="150"
          />
          <el-table-column align="left" label="服务对象" width="200">
            <template #default="{ row }">
              <a :href="row.StoreUrl" target="_blank">
                {{ row.BindStoreAccount }}
              </a>
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="200"
          /> -->
          <el-table-column align="center" label="服务周期" width="230">
            <template #default="{ row }">
              {{ row.ServiceStartDate }}至{{ row.ServiceEndDate }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="分单状态" width="150">
            <span style="color: #1890ff">新分单待接单</span>
          </el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="操作"
            width="100"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="receiveTask(row)">
                接单
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="接单待服务" name="await">
        <el-table v-loading="listLoading" :data="list">
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="OrderID"
            label="合同ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="OrderCode"
            label="合同编号"
            width="200"
          />
          <el-table-column align="center" prop="OrderType" label="合同属性" />
          <el-table-column align="center" label="派单时间" width="110">
            <template #default="{ row }">
              {{ row.AddTime.split(' ')[0] }}
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="AddTime"
            label="派单时间"
            width="200"
          />
          <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="200"
          /> -->
          <el-table-column align="left" label="客户名称" width="250">
            <template #default="{ row }">
              <div style="cursor: pointer" @click="getCustomerInfo(row)">
                {{ row.CustomerAllName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="KefuStaffName"
            label="续签客服"
            width="100"
          />
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="150"
          />
          <el-table-column align="left" label="服务对象" width="200">
            <template #default="{ row }">
              <a :href="row.StoreUrl" target="_blank">
                {{ row.BindStoreAccount }}
              </a>
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="200"
          /> -->
          <el-table-column align="center" label="服务周期" width="230">
            <template #default="{ row }">
              {{ row.ServiceStartDate }}至{{ row.ServiceEndDate }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="分单状态" width="150">
            <span style="color: #1890ff">接单待服务</span>
          </el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="查看任务并处理"
            width="130"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="运营服务中" name="serviceing">
        <el-table v-loading="listLoading" :data="list">
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="OrderID"
            label="合同ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="OrderCode"
            label="合同编号"
            width="200"
          />
          <el-table-column align="center" prop="OrderType" label="合同属性" />
          <el-table-column align="center" label="派单时间" width="110">
            <template #default="{ row }">
              {{ row.AddTime.split(' ')[0] }}
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="AddTime"
            label="派单时间"
            width="200"
          />
          <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="200"
          /> -->
          <el-table-column align="left" label="客户名称" width="250">
            <template #default="{ row }">
              <div style="cursor: pointer" @click="getCustomerInfo(row)">
                {{ row.CustomerAllName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="KefuStaffName"
            label="续签客服"
            width="100"
          />
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="150"
          />
          <el-table-column align="left" label="服务对象" width="200">
            <template #default="{ row }">
              <a :href="row.StoreUrl" target="_blank">
                {{ row.BindStoreAccount }}
              </a>
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="200"
          /> -->
          <el-table-column align="center" label="服务周期" width="230">
            <template #default="{ row }">
              {{ row.ServiceStartDate }}至{{ row.ServiceEndDate }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="分单状态" width="150">
            <span style="color: #67c23a">已接单服务中</span>
          </el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="查看任务并处理"
            width="130"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="运营暂停中" name="paused">
        <el-table v-loading="listLoading" :data="list">
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
          />
          <!-- <el-table-column
            align="center"
            prop="AddTime"
            label="派单时间"
            width="200"
          />
          <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="200"
          /> -->
          <el-table-column align="center" label="派单时间" width="100">
            <template #default="{ row }">
              {{ row.AddTime.split(' ')[0] }}
            </template>
          </el-table-column>
          <el-table-column align="left" label="客户名称" width="250">
            <template #default="{ row }">
              <div style="cursor: pointer" @click="getCustomerInfo(row)">
                {{ row.CustomerAllName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="KefuStaffName"
            label="续签客服"
            width="100"
          />
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="150"
          />
          <el-table-column align="center" label="服务对象" width="200">
            <template #default="{ row }">
              <a :href="row.StoreUrl" target="_blank">
                {{ row.BindStoreAccount }}
              </a>
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="200"
          /> -->
          <el-table-column align="center" label="服务周期" width="230">
            <template #default="{ row }">
              {{ row.ServiceStartDate }}至{{ row.ServiceEndDate }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="分单状态" width="150">
            <span style="color: #fa8c16">暂停服务中</span>
          </el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="查看任务并处理"
            width="200"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已终止服务" name="terminated">
        <el-table v-loading="listLoading" :data="list">
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
          />
          <!-- <el-table-column
            align="center"
            prop="AddTime"
            label="派单时间"
            width="200"
          /> -->
          <el-table-column align="center" label="派单时间" width="100">
            <template #default="{ row }">
              {{ row.AddTime.split(' ')[0] }}
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="200"
          /> -->
          <el-table-column align="left" label="客户名称" width="250">
            <template #default="{ row }">
              <div style="cursor: pointer" @click="getCustomerInfo(row)">
                {{ row.CustomerAllName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="150"
          />
          <el-table-column align="left" label="服务对象" width="200">
            <template #default="{ row }">
              <a :href="row.StoreUrl" target="_blank">
                {{ row.BindStoreAccount }}
              </a>
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="200"
          /> -->
          <el-table-column align="center" label="服务周期" width="230">
            <template #default="{ row }">
              {{ row.ServiceStartDate }}至{{ row.ServiceEndDate }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="分单状态" width="150">
            <span style="color: #ff0000">已终止服务</span>
          </el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="查看任务并处理"
            width="130"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="所有服务中客户" name="allServiceingCustomer">
        <el-table v-loading="listLoading" :data="list">
          <el-table-column
            align="center"
            prop="CustomerID"
            label="客户ID"
            width="80"
          />
          <el-table-column align="left" label="客户名称">
            <template #default="{ row }">
              <span style="cursor: pointer" @click="getCustomerInfo(row)">
                {{ row.CustomerAllName }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            label="所属区域"
            prop="CityShow"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="所属行业"
            prop="IndustryShow"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="地址"
            prop="Address"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="客户标记" width="200">
            <template #default="{ row }">
              <el-tag v-for="(itm, idx) in row.CustomerMarks" :key="idx">
                {{ itm }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column align="left" label="续签客服">
            <template #default="{ row }">
              {{ row.KefuStaffName }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="查看客户总览"
            width="130"
          >
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                @click="getCustomerInfo(row)"
              >
                查看客户总览
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <edit ref="editRef" @fetch-data="fetchData" />
    <customer-allInfo ref="CustomerAllInfoRef" />
    <!-- <el-dialog
      v-model="dialogFormVisible"
      title="接单"
      width="500px"
      @close="dialogClose"
    >
      <el-form-item label="设定服务期限">
        <el-date-picker
          v-model="queryForm.SeviceDates"
          type="daterange"
          start-placeholder="服务开始日期"
          end-placeholder="服务结束日期"
        />
      </el-form-item>
      <template #footer>
        <el-button @click="dialogClose">取 消</el-button>
        <el-button type="primary" @click="receiveTask(queryForm)">
          确 定
        </el-button>
      </template>
    </el-dialog> -->
  </div>
</template>
<script>
  // import DateSelect from '/src/components/DateSelect.vue'
  import {
    CitySelect,
    IndustrySelect,
    BusinessClassSelect,
    ProductClassSelect,
  } from '@/api/common'
  import { getList, doDelete, OperateGetOrder } from '@/api/storeOperate'
  import Edit from './components/StoreEdit'
  // import TopTip from '@/components/TopTip'
  import CustomerAllInfo from '@/views/components/CustomerAllInfo.vue'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'RenewalOperateOrder',
    components: { Edit, CustomerAllInfo },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        tipcontent: {
          title: '运营任务',
          memo:
            '本页用于“运营师”接收所被分配的工作任务；默认显示尚未接受的运营任务，收到后先进行接受，进入到处理中，并相应进行任务的跟进记录；' +
            '处理完成后，提交相关信息，等待管理员审核。',
        },
        CustomerAllInfoRef: null,
        editRef: null,
        list: [],
        clickList: {},
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        DateSelectRef: null,
        cityOptions: [],
        industryOptions: [],
        citySelect: { checkStrictly: true, multiple: true },
        industrySelect: { checkStrictly: true, multiple: true },
        orderTypeOption: [],
        ProductClassOptions: [],
        ProductClassSelect: { expandTrigger: 'hover', multiple: true },
        dialogFormVisible: false,
        selectRows: '',
        queryForm: {
          dataType: 'storeManager',
          tab: 'new',
          AssignOrderSetID: 13,
          TaskState: 0,
          pageNo: 1,
          pageSize: 10,
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const getTackinfo = (row) => {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        const actstaffpostid = actstaffpost[0][1]
        router.push({
          path: 'storeDetails',
          query: {
            actstaffpostid: actstaffpostid,
            OrderID: row.BusinessOrderID,
            WorkTaskID: row.WorkTaskID,
          },
        })
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }

      const toRecord = (row) => {
        state.clickList = row
        state.dialogFormVisible = true
      }
      // 领取任务
      const receiveTask = async (row) => {
        console.log(row, '1111')
        // if (!row.SeviceDates) {
        //   $baseMessage('请选择服务期限', 'error', 'vab-hey-message-error')
        // } else {
        //   const dataMsg = {
        //     WorkTaskID: state.clickList.WorkTaskID,
        //     SeviceDates: row.SeviceDates,
        //   }
        //   const { msg } = await OperateGetOrder(dataMsg)
        //   $baseMessage(msg, 'success', 'vab-hey-message-success')
        //   state.dialogFormVisible = false
        //   state.clickList = {}
        //   fetchData()
        // }
        const dataMsg = {
          WorkTaskID: row.WorkTaskID,
        }
        const { msg } = await OperateGetOrder(dataMsg)
        $baseMessage(msg, 'success', 'vab-hey-message-success')
        state.dialogFormVisible = false
        state.clickList = {}
        state.queryForm.tab = 'await'
        fetchData()
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        state.listLoading = true
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        state.ActStaffPostID = actstaffpost[0][1]
        const {
          data: { OrderTypeOption },
        } = await BusinessClassSelect()
        state.orderTypeOption = OrderTypeOption
        const {
          data: { ProductClassOptions },
        } = await ProductClassSelect()
        state.ProductClassOptions = ProductClassOptions
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }

      const dialogClose = () => {
        state.dialogFormVisible = false
      }
      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }
      onMounted(() => {
        fetchData()
        getCitySelect()
        getIndustrySelect()
      })

      const getCitySelect = async () => {
        const {
          data: { cityOptions },
        } = await CitySelect()
        state.cityOptions = cityOptions
        console.log('列表1', cityOptions)
      }
      const getIndustrySelect = async () => {
        const {
          data: { industryOptions },
        } = await IndustrySelect()
        state.industryOptions = industryOptions
        console.log('列表2', industryOptions)
      }

      const getDate = (data) => {
        state.queryForm.orderDate = data
        console.log(data, '子组件传过来的时间数组')
      }

      const getCustomerInfo = (row) => {
        state.CustomerAllInfoRef.showEdit(row)
        // router.push({
        //   path: '/customer/Info',
        //   query: { customerid: row.CustomerID },
        // })
      }

      return {
        ...toRefs(state),
        setSelectRows,
        dialogClose,
        getTackinfo,
        handleTabClick,
        receiveTask,
        toRecord,
        handleEdit,
        getDate,
        getCitySelect,
        getIndustrySelect,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        getCustomerInfo,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
